package com.example.myprogressview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * 进度指示器
 */
public class MyProgressView extends View {

    private static final String TAG = "MyProgressView";

    //控件的高度
    private int mHeight;

    //绘制的起始点
    private float mStartY = DEFAULT_START_Y;
    public static final float DEFAULT_START_Y = 0;
    private Context mContext;

    //实线的宽度
    private float mLineWidth;

    //圆环的半径
    private float mRadius;

    //圆环的位置（圆心）
    private float[] mCirclePositions = new float[3];

    //当前流程
    private int mProgress = -1;

    //画笔
    private Paint mLinePaint;
    private Paint innerCirclePaint;
    private Paint outerCirclePaint;

    public MyProgressView(Context context) {
        this(context, null);
    }

    public MyProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        //初始化属性
        initAttrs(context, attrs);
        //初始化画笔
        initPaints();
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyProgressView);
        //圆环的半径
        int defaultRadius = SizeUtil.dip2px(mContext,5);
        mRadius = defaultRadius ;
        //实线的宽度
        int defaultWidth = SizeUtil.dip2px(mContext,2);
        mLineWidth =  defaultWidth ;
        /*
         * 三个圆环的位置（y轴）
         * 推荐使用setCirclePositions方法设置
         */
        mCirclePositions[0] = -1;
        mCirclePositions[1] = -1;
        mCirclePositions[2] = -1;
        //回收
        a.recycle();
    }


    /**
     * 初始化画笔
     */
    private void initPaints() {
        //初始化实线画笔
        mLinePaint = new Paint();
        mLinePaint.setColor(Color.parseColor("#FF0000"));
        mLinePaint.setStrokeWidth(mLineWidth);
        //初始化圆形画笔
        innerCirclePaint = new Paint();
        outerCirclePaint = new Paint();
        innerCirclePaint.setColor(Color.parseColor("#FFFFFF"));
        outerCirclePaint.setColor(Color.parseColor("#D0021B"));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        Log.i(TAG, "heightSize --> " + heightSize);
        Log.i(TAG, "widthSize --> "+ widthSize);
        Log.i(TAG, "heightMode --> " + heightMode);
        Log.i(TAG, "widthMode --> " + widthMode);
        Log.i(TAG, "----------------------");
        mHeight = heightSize;
        if (heightMode == MeasureSpec.EXACTLY) {
            setMeasuredDimension((int) mRadius * 2, heightSize);
        } else {
            setMeasuredDimension((int) mRadius * 2, 0);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.i(TAG,"-- onDraw --");
        super.onDraw(canvas);
        //绘制实线
        canvas.drawLine(mRadius, mStartY, mRadius, mHeight, mLinePaint);
        //绘制圆环
        if (mCirclePositions != null && mCirclePositions.length > 0) {
            for (int i = 1; i <= 3; i ++) {
                float f = mCirclePositions[i - 1];
                if (f == -1) continue;  //未设值，不绘制
                canvas.drawCircle(mRadius, f, mRadius, outerCirclePaint);
                if (i != mProgress) {
                    canvas.drawCircle(mRadius, f, mRadius / 2, innerCirclePaint);
                }
            }
        }
    }

    /**
     * 设置当前进度
     * @param num
     */
    public void setProgress(int num) {
        if (num < 1 || num > 3) return;
        mProgress = num;
        invalidate();
    }

    /**
     * 设置View的高度
     * @param height
     */
    public void setHeight(int height) {
        mHeight = height;
    }

    /**
     * 设置绘制的Y轴起始点
     * @param y
     */
    public void setStartY(float y) {
        mStartY = y;
    }

    /**
     * 设置圆环的半径
     * @param radius
     */
    public void setRadius(float radius) {
        mRadius = radius;
    }

    public float getRadius() {
        return mRadius;
    }

    /**
     * 设置实线的宽度
     * @param width
     */
    public void setLineWidth(float width) {
        mLineWidth = width;
        mLinePaint.setStrokeWidth(mLineWidth);
    }

    /**
     * 设置圆环的位置
     * @param floats
     */
    public void setCirclePositions(float[] floats) {
        this.mCirclePositions = floats;
    }
}

